<template>
  <view class="articles-page">
    <!-- 顶部导航栏 -->
    <view class="navbar">
      <view class="navbar-left" @click="navigateBack">
        <uni-icons type="left" size="22" color="#666"></uni-icons>
      </view>
      <view class="navbar-title">文章资讯</view>
      <view class="navbar-right">
        <uni-icons type="search" size="22" color="#666" @click="showSearch"></uni-icons>
      </view>
    </view>

    <!-- 分类标签栏 -->
    <scroll-view class="category-scroll" scroll-x>
      <view class="category-tabs">
        <view 
          class="category-tab" 
          v-for="(category, index) in categories" 
          :key="index"
          @click="switchCategory(index)"
          :class="{ active: currentCategoryIndex === index }"
        >
          <text>{{ category.name }}</text>
          <view class="category-indicator" v-if="currentCategoryIndex === index"></view>
        </view>
      </view>
    </scroll-view>

    <!-- 文章列表内容 -->
    <scroll-view class="articles-content" scroll-y>
      <!-- 每个分类的文章列表 -->
      <view v-for="(category, catIndex) in categories" :key="catIndex">
        <view v-if="currentCategoryIndex === catIndex" class="category-container">
          <!-- 分类头部 -->
          <view class="category-header">
            <view class="category-header-title">{{ category.name }}</view>
            <view class="category-header-desc">{{ category.description }}</view>
          </view>

          <!-- 文章列表 -->
          <view class="article-list">
            <!-- 置顶文章 -->
            <view 
              class="article-item sticky-article" 
              v-if="category.stickyArticle"
              @click="gotoArticleDetail(category.stickyArticle.id)"
            >
              <view class="sticky-tag">置顶</view>
              <view class="article-main">
                <view class="article-title">{{ category.stickyArticle.title }}</view>
                <view class="article-meta">
                  <view class="article-author">{{ category.stickyArticle.author }}</view>
                  <view class="article-time">{{ category.stickyArticle.time }}</view>
                  <view class="article-views">
                    <uni-icons type="eye" size="14" color="#999"></uni-icons>
                    <text>{{ category.stickyArticle.views }}</text>
                  </view>
                </view>
              </view>
            </view>

            <!-- 普通文章 -->
            <view 
              class="article-item" 
              v-for="(article, artIndex) in category.articles" 
              :key="artIndex"
              @click="gotoArticleDetail(article.id)"
            >
              <!-- 带图片的文章 -->
              <view class="article-with-image" v-if="article.imageUrl">
                <view class="article-info">
                  <view class="article-title">{{ article.title }}</view>
                  <view class="article-meta">
                    <view class="article-author">{{ article.author }}</view>
                    <view class="article-time">{{ article.time }}</view>
                    <view class="article-views">
                      <uni-icons type="eye" size="14" color="#999"></uni-icons>
                      <text>{{ article.views }}</text>
                    </view>
                  </view>
                </view>
                <view class="article-image">
                  <image 
                    :src="article.imageUrl" 
                    mode="aspectFill"
                    :alt="article.title"
                    class="article-img"
                  ></image>
                </view>
              </view>

              <!-- 无图片的文章 -->
              <view class="article-no-image" v-else>
                <view class="article-title">{{ article.title }}</view>
                <view class="article-meta">
                  <view class="article-author">{{ article.author }}</view>
                  <view class="article-time">{{ article.time }}</view>
                  <view class="article-views">
                    <uni-icons type="eye" size="14" color="#999"></uni-icons>
                    <text>{{ article.views }}</text>
                  </view>
                </view>
              </view>
            </view>
          </view>

          <!-- 加载更多 -->
          <view class="load-more" @click="loadMoreArticles(catIndex)">
            <uni-icons 
              type="spinner-cycle" 
              size="16" 
              color="#999" 
              class="loading-icon" 
              v-if="loadingMore"
            ></uni-icons>
            <text>{{ loadingMore ? '加载中...' : '加载更多' }}</text>
          </view>
        </view>
      </view>

      <!-- 空状态 -->
      <view v-if="currentCategory.articles.length === 0 && !currentCategory.stickyArticle" class="empty-state">
        <uni-icons type="bookmark-o" size="60" color="#e0e0e0"></uni-icons>
        <text class="empty-text">暂时没有相关文章呢</text>
        <text class="empty-subtext">换个分类看看吧~</text>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentCategoryIndex: 0,
      loadingMore: false,
      categories: [
        {
          id: 1,
          name: "全部",
          description: "所有类别的精选文章",
          stickyArticle: {
            id: 1001,
            title: "如何有效提升微信小程序的用户体验与性能优化技巧",
            author: "张小明",
            time: "2023-06-18",
            views: "2.5k"
          },
          articles: [
            {
              id: 101,
              title: "Uniapp开发实战：从入门到精通的完整指南",
              author: "李华",
              time: "2023-06-15",
              views: "1.2k",
              imageUrl: "https://picsum.photos/id/1/300/200"
            },
            {
              id: 102,
              title: "微信小程序最新功能解读与应用场景分析",
              author: "王强",
              time: "2023-06-12",
              views: "986"
            },
            {
              id: 103,
              title: "前端性能优化的10个实用技巧",
              author: "赵静",
              time: "2023-06-10",
              views: "1.5k",
              imageUrl: "https://picsum.photos/id/2/300/200"
            },
            {
              id: 104,
              title: "响应式设计在移动端的最佳实践",
              author: "孙伟",
              time: "2023-06-08",
              views: "852"
            }
          ]
        },
        {
          id: 2,
          name: "技术",
          description: "最新的技术动态和开发技巧",
          stickyArticle: null,
          articles: [
            {
              id: 201,
              title: "JavaScript ES6+ 新特性全解析",
              author: "陈明",
              time: "2023-06-16",
              views: "2.1k",
              imageUrl: "https://picsum.photos/id/3/300/200"
            },
            {
              id: 202,
              title: "CSS Grid 布局完全指南",
              author: "林小红",
              time: "2023-06-14",
              views: "1.8k"
            },
            {
              id: 203,
              title: "Vue3 组合式API实战教程",
              author: "杨光",
              time: "2023-06-11",
              views: "1.3k",
              imageUrl: "https://picsum.photos/id/4/300/200"
            }
          ]
        },
        {
          id: 3,
          name: "设计",
          description: "UI/UX设计灵感与案例分析",
          stickyArticle: {
            id: 3001,
            title: "2023年移动应用设计趋势预测",
            author: "刘敏",
            time: "2023-06-17",
            views: "1.7k"
          },
          articles: [
            {
              id: 301,
              title: "移动端交互设计的7个基本原则",
              author: "周婷",
              time: "2023-06-15",
              views: "1.1k",
              imageUrl: "https://picsum.photos/id/5/300/200"
            },
            {
              id: 302,
              title: "色彩心理学在UI设计中的应用",
              author: "吴刚",
              time: "2023-06-10",
              views: "956"
            }
          ]
        },
        {
          id: 4,
          name: "资讯",
          description: "行业动态和最新资讯",
          stickyArticle: null,
          articles: [
            {
              id: 401,
              title: "微信小程序生态发展报告2023",
              author: "郑华",
              time: "2023-06-18",
              views: "2.3k",
              imageUrl: "https://picsum.photos/id/6/300/200"
            },
            {
              id: 402,
              title: "前端开发行业薪资调查报告",
              author: "钱明",
              time: "2023-06-16",
              views: "1.9k"
            }
          ]
        }
      ]
    };
  },
  computed: {
    currentCategory() {
      return this.categories[this.currentCategoryIndex] || { articles: [] };
    }
  },
  methods: {
    navigateBack() {
      uni.navigateBack({ delta: 1 });
    },
    showSearch() {
      uni.showToast({ title: "搜索功能待实现", icon: "none" });
    },
    switchCategory(index) {
      this.currentCategoryIndex = index;
    },
    gotoArticleDetail(articleId) {
      uni.navigateTo({ url:'../../contentPackage/pages/content/textDetail' });
    },
    loadMoreArticles(categoryIndex) {
      if (this.loadingMore) return;
      this.loadingMore = true;
      setTimeout(() => {
        const category = this.categories[categoryIndex];
        const newArticles = category.articles.slice(0, 2).map(article => ({
          ...article,
          id: article.id + 1000,
          time: "2023-05-20",
          views: (Math.random() * 1000).toFixed(0)
        }));
        this.categories[categoryIndex].articles = [...category.articles, ...newArticles];
        this.loadingMore = false;
      }, 1000);
    }
  }
};
</script>

<style scoped lang="scss">
.articles-page {
  background-color: #f8f9fa; /* 更柔和的背景色，减少视觉压力 */
  min-height: 100vh;
}

// 导航栏样式
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px; /* 增加高度，提升触摸舒适度 */
  padding: 0 18px;
  background-color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03); /* 极淡阴影，增加层次感但不突兀 */
  
  .navbar-left, .navbar-right {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%; /* 圆形点击区域，提升安全感 */
    transition: background-color 0.2s;
    
    &:active {
      background-color: #f1f3f5; /* 点击反馈更柔和 */
    }
  }
  
  .navbar-title {
    font-size: 18px;
    font-weight: 500;
    color: #343a40;
    flex: 1;
    text-align: center;
  }
}

// 分类标签栏
.category-scroll {
  background-color: #fff;
  padding: 8px 0; /* 增加上下内边距，让标签不拥挤 */
  white-space: nowrap;
  
  .category-tabs {
    display: inline-flex;
    padding: 0 18px;
    
    .category-tab {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 0 18px;
      height: 36px; /* 降低高度，减少压迫感 */
      justify-content: center;
      font-size: 15px;
      color: #6c757d;
      position: relative;
      transition: color 0.2s;
      
      &.active {
        color: #495057; /* 选中态用深灰色，更温和 */
        font-weight: 500;
      }
      
      .category-indicator {
        width: 20px;
        height: 2px;
        background-color: #495057;
        border-radius: 1px;
        position: absolute;
        bottom: 0;
        transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1); /* 指示器动画，增加流畅感 */
      }
    }
  }
}

// 文章列表内容区域
.articles-content {
  height: calc(100vh - 97px);
  padding-bottom: 24px;
  
  .category-container {
    animation: fadeIn 0.3s ease-out; /* 切换分类时的淡入动画，减少突兀感 */
  }
  
  .category-header {
    padding: 24px 18px 16px;
    
    .category-header-title {
      font-size: 22px;
      font-weight: 600;
      color: #212529;
      margin-bottom: 6px;
      letter-spacing: 0.3px; /* 轻微字间距，提升可读性 */
    }
    
    .category-header-desc {
      font-size: 14px;
      color: #6c757d;
      line-height: 1.5;
    }
  }
  
  .article-list {
    padding: 0 18px;
    
    .article-item {
      background-color: #fff;
      border-radius: 12px; /* 更大圆角，更柔和 */
      padding: 18px;
      margin-bottom: 16px; /* 增加间距，呼吸感 */
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.02); /* 极淡阴影，突出卡片但不刺眼 */
      transition: all 0.2s ease; /* 统一过渡效果 */
      position: relative;
      
      &:active {
        transform: scale(0.99); /* 轻微缩放，反馈更细腻 */
        box-shadow: 0 1px 5px rgba(0, 0, 0, 0.01);
      }
      
      .article-title {
        font-size: 17px;
        color: #212529;
        line-height: 1.5; /* 增加行高，阅读更轻松 */
        margin-bottom: 14px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        letter-spacing: 0.2px;
      }
      
      .article-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 12px;
        color: #868e96;
        
        .article-author {
          flex: 1;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        
        .article-time {
          margin: 0 14px;
        }
        
        .article-views {
          display: flex;
          align-items: center;
          
          uni-icons {
            margin-right: 4px;
          }
        }
      }
    }
    
    // 置顶文章
    .sticky-article {
      border-left: 3px solid #6c757d; /* 用深灰替代蓝色，减少攻击性 */
      
      .sticky-tag {
        position: absolute;
        top: 18px;
        right: 18px;
        background-color: #f1f3f5;
        color: #6c757d; /* 低饱和标签，不抢注意力 */
        font-size: 12px;
        padding: 2px 8px;
        border-radius: 4px;
      }
      
      .article-main {
        padding-right: 50px;
      }
    }
    
    // 带图片的文章
    .article-with-image {
      display: flex;
      position: relative;
      
      .article-info {
        flex: 1;
        padding-right: 16px;
      }
      
      .article-image {
        width: 110px;
        height: 88px;
        border-radius: 8px; /* 图片圆角，与卡片呼应 */
        overflow: hidden;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        
        .article-img {
          width: 100%;
          height: 100%;
          transition: transform 0.3s ease; /* 图片悬停放大，增加灵动感 */
        }
        
        &:active .article-img {
          transform: scale(1.05);
        }
      }
    }
    
    // 无图片的文章
    .article-no-image {
      position: relative;
    }
  }
  
  // 加载更多
  .load-more {
    text-align: center;
    padding: 14px;
    margin: 0 18px 16px;
    font-size: 14px;
    color: #6c757d;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.02);
    transition: background-color 0.2s;
    
    &:active {
      background-color: #f8f9fa;
    }
    
    .loading-icon {
      margin-right: 6px;
      vertical-align: middle;
    }
  }
  
  // 空状态（情感化设计）
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 320px;
    margin-top: 40px;
    padding: 0 20px;
    text-align: center;
    
    .empty-text {
      margin-top: 20px;
      font-size: 15px;
      color: #6c757d;
    }
    
    .empty-subtext {
      margin-top: 8px;
      font-size: 13px;
      color: #adb5bd;
    }
  }
}

// 切换分类的淡入动画
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>